<?php

namespace app\apiz\controller\user;

use think\Controller;
use think\Cookie;

class Address extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->UserModel = model('User.User');
        $this->AddressModel = model('User.Address');
        $this->RegionModel = model('Region');
    }
    
    public function index()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid',0);
            $User = $this->UserModel->find($userid);

            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }

            $list = $this->AddressModel->with(['provinces', 'citys', 'districts'])->where(['userid' => $userid])->select();

            $this->success('返回收货地址成功',null,$list);
            exit;

        }
    }

    public function add()
    {
        if($this->request->isPost())
        {
            $params = $this->request->param();
            $userid = $this->request->param('userid',0);
            $code = $this->request->param('code',0);

            $User = $this->UserModel->find($userid);
            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }
            //组装数据
            $data = [
                'consignee' => $params['consignee'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
                'status' => $params['status'],
                'userid' => $params['userid'],
            ];

            $parentpath = $this->RegionModel->where(['code' => $code])->value('parentpath');

            if(empty($parentpath))
            {
                $this->error('您选择的地区不存在,请重新选择');
                exit;
            }

            //将字符串转换为数组
            $path = explode(',', $parentpath);

            if(count($path) <= 0)
            {
                $this->error('您所选择的地区不存在,请重新选择');
                exit;
            }

            //将数组里面的值分别给到不同的变量
            list($data['province'], $data['city'], $data['district']) = $path;

            //判断是否选择了默认地址
            if($data['status'] == 1)
            {
                //将这个人所有的收货地址都要改成非默认
                $UpdateStatus = $this->AddressModel->where(['userid' => $userid])->update(['status' => 0]);

                if($UpdateStatus === FALSE)
                {
                    $this->error('更新默认收货地址失败');
                    exit;
                }
            }

            $result = $this->AddressModel->validate('common/User/Address')->save($data);

            if($result == FALSE)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }else
            {
                $this->success('添加收货地址成功');
                exit;
            }
        }
    }

    public function info()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $addrid = $this->request->param('addrid', 0);

            $User = $this->UserModel->find($userid);

            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }

            $address = $this->AddressModel->with(['provinces', 'citys', 'districts'])->find($addrid);

            if(!$address)
            {
                $this->error('收货地址不存在');
                exit;
            }

            if($address['userid'] != $userid)
            {
                $this->error('非法修改他人地址，无权修改');
                exit;
            }

            $this->success('返回收货地址成功', null, $address);
            exit;
        }
    }

    public function edit()
    {
        if($this->request->isPost())
        {
            $params = $this->request->param();
            $addrid = $this->request->param('addrid',0);
            $userid = $this->request->param('userid',0);
            $code = $this->request->param('code',0);

            $User = $this->UserModel->find($userid);

            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }

            $address = $this->AddressModel->find($addrid);

            if(!$address)
            {
                $this->error('收货地址不存在');
                exit;
            }

            if($address['userid'] != $userid)
            {
                $this->error('非法修改他人地址');
                exit;
            }

            //组装数据
            $data = [
                'id' => $addrid,
                'consignee' => $params['consignee'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
                'status' => $params['status'],
                'userid' => $params['userid'],
            ];

            //查询地区数据
            $parentpath = $this->RegionModel->where(['code' => $code])->value('parentpath');

            if(empty($parentpath))
            {
                $this->error('您所选择的地区不存在,请重新选择');
                exit;
            }

            //将字符串转换为数组
            $path = explode(',', $parentpath);

            if(count($path) <= 0)
            {
                $this->error('您所选择的地区不存在,请重新选择');
                exit;
            }
            
            list($data['province'], $data['city'], $data['district']) = $path;

            //判断是否选择了默认地址
            if($data['status'] == 1)
            {
                //将这个人所有的收货地址都要改成非默认
                $UpdateStatus = $this->AddressModel->where(['userid' => $userid])->update(['status' => 0]);

                if($UpdateStatus === FALSE)
                {
                    $this->error('更新默认收货地址失败');
                    exit;
                }
            }

            //更新数据库
            $result = $this->AddressModel->validate('common/User/Address')->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }else
            {
                $this->success('更新收货地址成功');
                exit;
            }

        }
    }

    public function delete()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $addrid = $this->request->param('addrid', 0);

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            $address = $this->AddressModel->find($addrid);

            if(!$address)
            {
                $this->error('收货地址不存在');
                exit;
            }

            if($address['userid'] != $userid)
            {
                $this->error('非法修改他人地址，无权修改');
                exit;
            }

            //删除选中的地址
            $result = $this->AddressModel->destroy($addrid);

            if($result === FALSE)
            {
                $this->error('删除失败');
                exit;
            }else
            {
                $this->success('删除成功');
                exit;
            }
        }
    }

    //切换默认收货地址
    public function check()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $addrid = $this->request->param('addrid', 0);

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            $address = $this->AddressModel->find($addrid);

            if(!$address)
            {
                $this->error('收货地址不存在');
                exit;
            }

            if($address['userid'] != $userid)
            {
                $this->error('非法修改他人地址，无权修改');
                exit;
            }


            //先把这个人的所有的收货地址都改成0 非默认 ，在改指定这个id地址

            //开启事务
            $this->AddressModel->startTrans();
            
            //更新
            $UpdateStatus = $this->AddressModel->where(['userid' => $userid])->update(['status' => 0]);

            if($UpdateStatus === FALSE)
            {
                $this->error('更新默认收货地址失败');
                exit;
            }

            //更新选中的地址
            $result = $this->AddressModel->where(['id' => $addrid])->update(['status' => 1]);

            if($result === FALSE)
            {
                //回滚事务
                $this->AddressModel->rollback();
                $this->error('更新默认地址失败');
                exit;
            }else
            {
                //提交事务
                $this->AddressModel->commit();
                $this->success('更新默认地址成功');
                exit;
            }
        }

    }
}